package com.xiongwei.protocol;

import com.xiongwei.protocol.register.LocalRegister;
import org.apache.tomcat.util.http.fileupload.IOUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.InvocationTargetException;

public class MyServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            InvocationParam invocationParam = (InvocationParam) new ObjectInputStream(req.getInputStream()).readObject();
            Class classImpl = LocalRegister.get(invocationParam.getInterfaceName());
            String result = (String)classImpl.getMethod(invocationParam.getMethodName(), invocationParam.getParamTypes())
                    .invoke(classImpl.newInstance(), invocationParam.getParams());
            new ObjectOutputStream(resp.getOutputStream()).writeObject(result);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        }
        System.out.println("doPost");
    }
}
